package com.aotain.common;

import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;


public class CommonFunction {

	/**
	 *
	 * @param cookievalue
	 * @return
	 */
	public static String getQQNumber(String cookievalue){
		try {
		  Pattern pattern = Pattern.compile("([1-9]\\d{4,11})");
	      Matcher  matcher = pattern.matcher(cookievalue);
	      if(matcher.find()){
	        cookievalue=matcher.group(0);
	    	}else{
	    	  cookievalue=null;
	      }
	      }catch(Exception e){
	    	  cookievalue=null;
	      }
	      return cookievalue;
	}
	public static int generateRandom(int a, int b) {
	        int temp = 0;
	        try {
	            if (a > b) {
	                temp = new Random().nextInt(a - b);
	                return temp + b;
	            } else {
	                temp = new Random().nextInt(b - a);
	                return temp + a;
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return temp + a;
	 }
	 
	 
	 /**
	  * 
	  *
	  * @param c 
	  * @return 
	  */
	 public static boolean isChinese(char c) {
	     Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
	     if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
	             || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
	             || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
	             || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
	             || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
	             || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
	         return true;
	     }
	     return false;
	 }
	  
	 /**
	  * 
	  *
	  * @param strName 
	  * @return 
	  */
	 public static boolean isMessyCode(String strName) {
		 
	     Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
	     Matcher m = p.matcher(strName);
	     String after = m.replaceAll("");
	     String temp = after.replaceAll("\\p{P}", "");
	     char[] ch = temp.trim().toCharArray();
	     float chLength = ch.length;
	     float count = 0;
	     for (int i = 0; i < ch.length; i++) {
	         char c = ch[i];
	         if (!Character.isLetterOrDigit(c)) {
	             if (!isChinese(c)) {
	                 count = count + 1;
	             }
	         }
	     }
	     float result = count / chLength;
	     if (result > 0.1) {
	         return true;
	     } else {
	         return false;
	     }
	  
	 }
	  
	 /**
	  * 
	  * @param str
	  * @param regEx
	  * @param group
	  * @return
	  */
	 public static String findByRegex(String str, String regEx, int group)
	 	{
	 		String resultValue = null;
	 		if ((str == null) || (regEx == null) || ((regEx != null) && ("".equals(regEx.trim())))) 
	 			return resultValue;
	 		
	 		
	 		Pattern p = Pattern.compile(regEx);
	 		Matcher m = p.matcher(str);

	 		boolean result = m.matches();
	 		if (result)
	 		{
	 			resultValue = m.group(group);
	 		}
	 		return resultValue;
	 	}
	 

	/**
	 * 
	 * @param strA
	 * @param strB
	 * @return
	 */
	public static float compare(String strA,String strB) {  
			 int d[][]; //
			  int n = strA.length();
			  int m = strB.length();
			  int i; // 
			  int j; // 
			  
			  //
			  //int matchlength = (int)((float)Math.min(n, m)*0.7);
			  //if(Math.min(n, m)<=4)
				//  matchlength = Math.min(n, m);
			  
			  
			  char ch1; // 
			  char ch2; // 
			  
			  
				 
			  int temp; // 
			  if (n == 0) {
				  return m;
			  }
			  if (m == 0) {
				  return n;
			  }
			 
			  //n = n - matchlength + 1;
			  //m = m - matchlength + 1;
			 
			  
			  d = new int[n + 1][m + 1];
			  for (i = 0; i <= n; i++) { // 
				  d[i][0] = i;
			  }
			 
			  for (j = 0; j <= m; j++) { // 
				  d[0][j] = j;
			  }
			 
			  for (i = 1; i <= n; i++) { // 
				  ch1 = strA.charAt(i - 1);
				  // 
				  for (j = 1; j <= m; j++) {
					  ch2 = strB.charAt(j - 1);
					  if (ch1==ch2) {
						  temp = 0;
					  } else {
						  temp = 1;
					  }
					  // 
					  d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
				  }
			  	}
			  	return d[n][m];
			 }
			 
	private static int min(int one, int two, int three) {
		return (one = one < two ? one : two) < three ? one : three;
	}
			 
	 /**
	  * Levenshtein 
	  * @param str
	  * @param target
	  * @return
	  */
	 public static float SimilarLevenshtein(String str, String target) {				 
		 //str = str.split(" ")[str.split(" ").length - 1];
		 //target = target.split(" ")[target.split(" ").length - 1];		 
		 if(Math.max(str.length(), target.length())<= 4)
		 {
			 if(str.equals(target))
				 return 1;	 
			 return 0;
		 }
		 return 1 - (float)compare(str, target)/Math.max(str.length(), target.length());
	 }
			 
	 /**
	  *  
	  * @param str1
	  * @param str2
	  * @return
	  */
	 private static int getCommonStrLength(String str1, String str2) {
		 str1 = str1.toLowerCase();  
		 str2 = str2.toLowerCase();  
		 int len1 = str1.length();  
		 int len2 = str2.length();  
		 String min = null;  
		 String max = null;  
		 String target = null;
		 min = len1 <= len2 ? str1 : str2;
		 max = len1 >  len2 ? str1 : str2;
		 // 
		 for (int i = min.length(); i >= 1; i--) {
			 //
			 for (int j = 0; j <= min.length() - i; j++) {  
				 target = min.substring(j, j + i);  
				 //
				 for (int k = 0; k <= max.length() - i; k++) {  
					 if (max.substring(k,k + i).equals(target)) {  
						 return i;  
					 }
				 }
			 }
		 }  
		 return 0;  
	}
			 
	/**
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static float SimilarCommonStrLength(String str1, String str2)
	{
		return (float)getCommonStrLength(str1,str2)/Math.max(str1.length() , str2.length()); 
	}
			 
	public static float Similar(String str1, String str2)
	{
		if(str2.length() == 0 || str1.length() ==0)
			return 0;
				 
		boolean next = false;
		if(str1.contains(str2))
		{
			next = true;
			//String str = StringUtils.rightPad(str2, str1.length() ,"0");
			//str1 = str1.replace(str2, str);
			//str2 = str;	 
		}
		else if(str2.contains(str1))
		{
			next = true;
			//String str = StringUtils.rightPad(str1, str2.length(),"0");
			//str2 = str2.replace(str1, str);
			//str1 = str;
		}
				 
		if(next)
		{// 
			float f = CommonFunction.SimilarCommonStrLength(str1,str2);
			return f;
		}
		return 0;
	}
			 
	public static String parseCookie(String cookieConfig,String domain,String cookie){
		String cookievalue="";
		if(cookieConfig.indexOf(domain)!=-1){
			String searchcookiename=cookieConfig.substring(cookieConfig.indexOf(domain));
			if(searchcookiename.indexOf("#")!=-1)
				searchcookiename=searchcookiename.substring(0,searchcookiename.indexOf("#"));
			searchcookiename=searchcookiename.split("=")[1];
			if((cookie.indexOf(searchcookiename)!=-1)){
				cookievalue=cookie.substring(cookie.indexOf(searchcookiename));
				if(cookievalue.indexOf(";")!=-1)
					cookievalue=cookievalue.substring(0,cookievalue.indexOf(";"));
				cookievalue=cookievalue.split("=")[1];
			}
		}
		return cookievalue;
	}
			 
			 
	/***
	 * Gets the URL domain name
	 * @param url      eg:http://www.jfox.info
	 * @return domain  eg:www.jfox.info
	 */
	 public static String getDomain(String url) {
		 try{
			 Pattern pattern = Pattern.compile("^((https|http|ftp|rtsp|mms|mp3|url)?://)");
			 Matcher matcher = pattern.matcher(url);
			 if (!matcher.find()) {
				 url = "http://" + url;
			 }
			 url = new URL(url).getHost().toLowerCase();
		 }catch(Exception ex){
			 return "";
		 }
		 return url;
	 }
				
	 /**
	  * Base64  
	  * @param s
	  * @return
	  */
	 public static String decodeBASE64(String s) { 
		 if (s == null)
			 return null; 
		 byte[] b = Base64.decodeBase64(s);
		 String sReturn = new String(b);
		 return sReturn;
	 }
				
	 /**
		 * 
		 * @param ipInt
		 * @return
		 */
		public static String int2ip(long ipInt){
			
			StringBuilder sb=new StringBuilder();
			sb.append((ipInt>>24)&0xFF).append(".");
			sb.append((ipInt>>16)&0xFF).append(".");
			sb.append((ipInt>>8)&0xFF).append(".");
			sb.append(ipInt&0xFF);
			return sb.toString(); 
			
		} 
		
				   
	/**
	 *  
	 * @param app
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public static String getAppParse(String app, String domain) throws Exception
	{
		String appclassid="",tempdom,temp;
		int pos = -1;
		String[] plist = app.split(";");
		for(int index=0;index<plist.length;index++)
		{
			temp = plist[index];
			tempdom = temp.substring(0,temp.indexOf("="));
			pos = domain.indexOf(tempdom);
			if(pos>-1)
			{
				appclassid = temp.substring(tempdom.length()+1,temp.length());
				break;
			}
		}
		if(appclassid.length()<1)
			appclassid = "12_0";
		return appclassid;
	}
			
	/**
	 * 
	 * @param plainText
	 * @return
	 */
	 public static String md5s(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
            	i = b[offset];
            	if (i < 0)
            		i += 256;
            	if (i < 16)
            		buf.append("0");
            	buf.append(Integer.toHexString(i));
            }
            String str = buf.toString();
            	return str;
					   //System.out.println("result: " + buf.toString());// 
					   //System.out.println("result: " + buf.toString().substring(8, 24));// 16位锟侥硷拷锟斤拷
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
			}
	 }
			 
	/**
	 * 
	 * @param urlconfig
	 * @return
	 */
	 public static HashMap<String,String> getUrlClassConfig(String urlconfig)
	 {
			String[] arrClass = urlconfig.split(";",-1);
			HashMap<String,String> hmClass = new HashMap<String, String>();
			for(String s : arrClass)
			{
				if(s.split("=",-1).length < 2)
					continue;
				hmClass.put(s.split("=",-1)[0],s.split("=",-1)[1]);
			}			
			return hmClass;
	 }
		
	 /**
	  *  
	  * @param hmConfig
	  * @param domain
	  * @return
	  */
	 public static String getUrlClass(HashMap<String,String> hmConfig,String domain)
	 {
			if(hmConfig.containsKey(domain))
			{
				return hmConfig.get(domain);
			}
			else
			{
				return "";
			}
	 }
			 
				
	// 
	public static String appparse(String app, String domain) throws Exception
	{
		String appclassid="",tempdom,temp;
		int pos = -1;
		String[] plist = app.split(";");
		for(int index=0;index<plist.length;index++)
		{
			temp = plist[index];
			tempdom = temp.substring(0,temp.indexOf("="));
			pos = domain.indexOf(tempdom);
			if(pos>-1)
			{
				appclassid = temp.substring(tempdom.length()+1,temp.length());
				break;
			}
		}
		if(appclassid.length()<1)
			appclassid = "12_0";
		return appclassid;
	}
	
	/**
	 * 
	 * @param url
	 * @param cf
	 * @return
	 */
	public static String parseKeyWord(String url,String cf){
		String kw="";
		String[] cols =cf.split("\\#");
		int exist = -1;
		int pos =0;
		for(String temp:cols){
			String[] rds = temp.split("=");
			if(url.contains(rds[0])){
				exist=0;
				break;
			}
			pos++;
		}
		if(exist == 0 ){
			String[] rd = cols[pos].split("\\=");
			boolean multiple =rd[1].contains(",");
			if(multiple){
				String[] mulp = rd[1].split(",");
				for(String temp:mulp){
					 if (url.indexOf("?"+temp+"=") > 0 || url.indexOf("&"+temp+"=") > 0)
		             {	
						 int tp =url.indexOf("?"+temp+"=") > 0 ? url.indexOf("?"+temp+"=") : url.indexOf("&"+temp+"=");
						 String  tempkw =url.substring(tp+temp.length()+2);
						 if(tempkw.contains("&")){
							 tempkw = tempkw.substring(0, tempkw.indexOf("&"));
						 }else{
							 tempkw=tempkw.substring(0);
						 }
						 byte[] buf = CommonFunction.GetUrlCodingToBytes(tempkw);
						 try {
							 if(CommonFunction.IsUTF8(buf)){
									kw = new String( java.net.URLDecoder.decode(tempkw,"utf-8"));
							}else{
								kw = new String( java.net.URLDecoder.decode(tempkw,"gbk"));
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						 break;
		             }
				}
			}else{
				 if (url.indexOf("?"+rd[1]+"=") > 0 || url.indexOf("&"+rd[1]+"=") > 0)
	             {
					 int tp =url.indexOf("?"+rd[1]+"=") > 0 ? url.indexOf("?"+rd[1]+"=") : url.indexOf("&"+rd[1]+"=");
					 String  tempkw =url.substring(tp + rd[1].length()+2);
					 if(tempkw.contains("&")){
						 tempkw = tempkw.substring(0, tempkw.indexOf("&"));
					 }else{
						 tempkw=tempkw.substring(0);
					 }
					byte[] buf = CommonFunction.GetUrlCodingToBytes(tempkw);
					 try {
						 if(CommonFunction.IsUTF8(buf)){
								kw = new String( java.net.URLDecoder.decode(tempkw,"utf-8"));
						}else{
							kw = new String( java.net.URLDecoder.decode(tempkw,"gbk"));
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	             }
			}
		}
		 return kw;
	   }


	/**
	 *  
	 * @param buf
	 * @return
	 */
	public static boolean IsUTF8(byte[] buf) { 

		int score = 0;

		int i, rawtextlen = 0;

		int goodbytes = 0, asciibytes = 0;

		rawtextlen = buf.length;

		for (i = 0; i < rawtextlen; i++) {
			if ((buf[i] & (byte) 0x7F) == buf[i]) {
				//  
				asciibytes++;
			} else if (-64 <= buf[i] && buf[i] <= -33
					&& i + 1 < rawtextlen && -128 <= buf[i + 1]
					&& buf[i + 1] <= -65) {
				goodbytes += 2;
				i++;

			} else if (-32 <= buf[i]&& buf[i] <= -17
			&&i + 2 < rawtextlen && -128 <= buf[i + 1]
			&& buf[i + 1] <= -65 && -128 <= buf[i + 2]
			&& buf[i + 2] <= -65) {
				goodbytes += 3;
				i += 2;
			}
		}
		if (asciibytes == rawtextlen) {
			return false;
		}
		score = 100 * goodbytes / (rawtextlen - asciibytes);
		if (score > 98) {
			return true;
		} else if (score > 95 && goodbytes > 30) {
			return true;
		} else {
			return false;
		}
	}
				
	/**
	 * url  
	 * @param url
	 * @return
	 */
	public static byte[] GetUrlCodingToBytes(String url)
	{
		StringBuilder sb = new StringBuilder();
		int i = url.indexOf("%");
		while (i >= 0)
		{
			if (url.length() < i + 3)
			{
				break;
			}
			sb.append(url.substring(i, i + 3));
			url = url.substring(i + 3);
			i = url.indexOf("%");
		}
		
		String urlCoding = sb.toString();
		if (urlCoding.isEmpty())
			return new byte[0];
		urlCoding = urlCoding.replace("%", "");

		int len = urlCoding.length() / 2;
		byte[] result = new byte[len];
		len *= 2;
		for (int index = 0; index < len; index++)
		{
			String s = urlCoding.substring(index, index + 2);
			int b = Integer.parseInt(s, 16);  
			
			//int b = int.Parse(s, System.Globalization.NumberStyles.HexNumber);
			result[index / 2] = (byte)b;
			index++;
		}
		return result;
	}	
	
	
	
	
	/** 
	 *  
	 * @param ip 
	 * @return 
	 */  
	public static Long ip2int(String ip)   
	{  
	    Long num = 0L;  
	    if (ip == null){  
	        return num;  
	    }  
	      
	    try{  
	        ip = ip.replaceAll("[^0-9\\.]", ""); //
	        String[] ips = ip.split("\\.");  
	        if (ips.length == 4){  
	            num = Long.parseLong(ips[0], 10) * 256L * 256L * 256L + Long.parseLong(ips[1], 10) * 256L * 256L + Long.parseLong(ips[2], 10) * 256L + Long.parseLong(ips[3], 10);  
	            num = num >>> 0;  
	        }  
	    }catch(NullPointerException ex){  
	        System.out.println(ip);  
	    }  
	      
	    return num;  
	}  
	
	
	/**
	  * 
	  * 将DPI还原HTTP回话后提取的域名转换成顶级域名
	  * <功能详细描述>
	  * @param domain
	  * @return
	  * @see [类、类#方法、类#成员]
	  */
	 public static String getTopDomain(String domain) {
		 	//判断是否属于局域网IP段范围内
			String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
			
			String domainname = domain;
			//处理域名后带有端口号的情况
			String[] domainValues = domainname.split(":");
			if(domainValues.length > 1)
			{
				domainname = domainValues[0];
			}
			
			String IP = CommonFunction.findByRegex(domainname, rexp, 0);
			String[] strArr = domainname.split("\\.",-1);
			
			if(strArr.length >= 2)
			{//根域名//顶级域名
				 //判断是否为IP地址
				 if(IP != null)
				 {//如果是IP，返回
					 //return null;
					 domainname = IP;
				 }
				 
				 else if(IsSpecialRoot(domainname) && strArr.length >= 3)
				 {//特殊处理例如 .com.cn  .com.hk
					 domainname = String.format("%s.%s.%s", strArr[strArr.length-3],
							 strArr[strArr.length-2],strArr[strArr.length-1]);
				 }
				 else if(IsSpecialRoot2Level(domainname))
				 {
					 domainname = String.format("%s.%s", 
							 strArr[strArr.length-2],strArr[strArr.length-1]);
				 }
				 else
				 {
					 domainname = "";
				 }
			}
			return domainname;
	 }
	 
	
	/**
	 * 是否满足正则表达式
	 * @param 
	 * @return
	 */
	public static boolean isRegex(String value,String reg){
        //String reg = "^([0-9a-fA-F]){2}([:][0-9a-fA-F]{2}){5}";
        return Pattern.compile(reg).matcher(value).find();
    }
	
	public static boolean IsSpecialRoot(String domain)
	 {
		 boolean blReturn = false;
		 if(domain.contains(".com.")
				 || domain.contains(".co.")
				 || domain.contains("gd.cn")
				 || domain.contains(".cn.com")
				 || domain.contains(".gov.")
				 || domain.contains(".net.")
				 || domain.contains(".edu.")
				 || domain.contains(".org.")
				 || domain.contains(".ac."))
			 blReturn = true;
		 return blReturn;
	 }
	
	public static boolean IsSpecialRoot2Level(String domain)
	 {
		 boolean blReturn = false;
		 if(domain.contains(".com")
				 || domain.contains("cn")
				 || domain.contains(".net")
				 || domain.contains(".org"))
			 blReturn = true;
		 return blReturn;
	 }
	
	/***
	 * Gets the URL domain name
	 * @param url      eg:http://www.jfox.info
	 * @return domain  eg:jfox.info
	 */
	public static String getRootDomain(String url){
		  String domain = "";
		  Pattern pattern = Pattern.compile("[\\w-]+\\.(com.cn|net.cn|gov.cn|org.cn|edu.cn|com.hk|net.hk|gov.hk|org.hk|edu.hk|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cf|cg|ch|ci|ck|cl|cm|cn|cq|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|es|et|ev|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp|gr|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|ml|mm|mn|mo|mp|mq|mr|ms|mt|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tw|tz|ua|ug|uk|us|uy|va|vc|ve|vg|vn|vu|wf|ws|ye|yu|za|zm|zr|zw|mobi|com|net|org|int|edu|gov|mil|arpa|Asia|biz|info|name|pro|coop|aero|museum|ac|cc|co|tv)$");
	      Matcher matcher = pattern.matcher(getDomain(url));
	      if (matcher.find()) {
	       domain = matcher.group();
	      }
	      if (domain == null || domain.trim().equals(""))
	       return null;
	      else {
	       return domain;
	      }
	}
	
	public static int MatchSQLInjection(String url,String injectionKeyList){
		
		int matchNum = 0;
		try{				
			if(url.isEmpty()) 
				return matchNum;
			//url = URLDecoder.decode(CommonFunction.decodeBASE64(url),"utf-8").toLowerCase();
			url = url.toLowerCase();
			if(url.contains("?") == true)
				return matchNum;
			url = url.substring(url.indexOf("?"),url.length()).replaceAll("'", "");
			//String rgx = "\\b(" + injectionKeyList + ")\\b";\
			String rgx = "(" + injectionKeyList + ")";
			Pattern pattern = Pattern.compile(rgx); 
			Matcher m = pattern.matcher(url);
			ArrayList<String> list = new ArrayList<String>(); 
			while(m.find()) {
				if(!list.contains(m.group())) list.add(m.group());
			}
			
			if(list.contains("order") && !list.contains("by")){
				matchNum = 1;
			}else if(list.contains("group") && !list.contains("by")){
				matchNum = 1;
			}else if(!list.contains("select") && list.size() < 3){
				matchNum = 1;
			}else{
				matchNum =  list.size();
			}
		}catch(Exception ex){
			Logger.getRootLogger().error("=====SQLInjectionBolt:matchRuleBase=========ERROR=================",ex);
		}		
		return matchNum;
	
	}
	
	public static int MatchInjection(String url,String injectionKeyList){
		
		int matchNum = 0;
		try{				
			if(url.isEmpty()) 
				return matchNum;
			//url = URLDecoder.decode(CommonFunction.decodeBASE64(url),"utf-8").toLowerCase();
			url = url.toLowerCase();
			if(url.indexOf("?") == -1) return matchNum;
			url = url.substring(url.indexOf("?"),url.length()).replaceAll("'", "");
			//String rgx = "\\b(" + injectionKeyList + ")\\b";\
			String rgx = "(" + injectionKeyList + ")";
			Pattern pattern = Pattern.compile(rgx); 
			Matcher m = pattern.matcher(url);
			ArrayList<String> list = new ArrayList<String>(); 
			while(m.find()) {
				if(!list.contains(m.group())) list.add(m.group());
			}
			
			matchNum =  list.size();
		}catch(Exception ex){
			Logger.getRootLogger().error("=====SQLInjectionBolt:matchRuleBase=========ERROR=================",ex);
		}		
		return matchNum;
	
	}
	
	
	/**
	 * 判断是否为本地局域网IP
	 * @param ip
	 * @return
	 * @Description:
	 */
	public static boolean IsLocalIP(Long ip) {
		if((ip >= 167772160 && ip <= 184549375) 
			|| (ip >= 2886729728L && ip <= 2887778303L)
			|| (ip >= 3232235520L && ip  <= 3232301055L)) {
			return true;
		} else {
			return false;
		}
	}
}
